<template>
	<view class="container">
		<uni-segmented-control :current="current" :values="items" style-type="text" active-color="#FA2C1E"
		 @clickItem="onClickItem"  style="background-color: #fff;"/>
		<view class="orders">
			<view class="order" v-for="(item,index) in orderList" :key="index" @click="toDetail(item.id)">
				<view class="top">
					<view class="order_no">
						订单编号：{{item.order_no}}
					</view>
					<view class="wait_time" v-if="state[current] == 1">
						<text class="wait">等待支付</text> <text class="time">{{item.wait_time}}</text>
					</view>
					<view class="state_text" v-if="state[current] == 2" style="color: #FA2C1E;font-size: 28rpx;" >
						待发货
					</view>
					<view class="state_text" v-if="state[current] == 3" style="color: #FA2C1E;font-size: 28rpx;" >
						待收货
					</view>
					<view class="state_text" v-if="state[current] == 4" style="color: #333333;font-size: 24rpx;">
						已完成/核销订单
					</view>
				</view>
				<view class="center">
					<view class="order_img">
						<image :src="item.goods[0] ? item.goods[0].image : ''" mode="aspectFit" class="img"></image>
					</view>
					<view class="center_right">
						<view class="name text-ellipsis">{{item.goods[0] ? item.goods[0].title : ''}}</view>
						<view class="price_info">
							<view class="price">{{item.pay ? item.pay.price: ''}}</view>
							<view class="number">x{{item.pay?item.pay.number:''}}</view>
						</view>
					</view>
				</view>
				<view class="order_time">下单时间:{{item.createtime_text}}</view>
				<view class="options">
					<button type="default" class="btn pay" v-if="state[current] == 1" @click.stop="getPay(item.id)">去付款</button>
					<button type="default" class="btn pay" v-if="state[current] == 3" @click.stop="confirmOrder(item.id)">确认收货</button>
					<button type="default" class="btn cancel" v-if="state[current] == 2 || state[current] == 3 || state[current] == 4" @click.stop="toDetail(item.id)">查看详情</button>
					<button type="default" class="btn cancel" v-if="state[current] == 1" @click.stop="cancelOrder(item.id)">取消订单</button>
				</view>
			</view>
		</view>
		<uni-load-more :status="status" :content-text="contentText" />
	</view>
</template>

<script>
	import {getOrderList,getPay,cancelOrder,payment,confirmOrder} from '../../api/api.js'
	export default {
		data() {
			return {
				current: 0,
				items: ['待支付', '待发货', '待收货','已完成'],
				// 订单状态:1=待支付,2=待发货,3=待收货,4=已完成  【前面是Index后面是订单状态】
				state:{ 0:1, 1:2, 2:3, 3:4 },
				page:1,
				orderList:[],
				status:'more',
				statusTypes: [{
					value: 'more',
					text: '加载前',
					checked: true
				}, {
					value: 'loading',
					text: '加载中',
					checked: false
				}, {
					value: 'noMore',
					text: '没有更多',
					checked: false
				}],
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				}
			}
		},
		onLoad(options) {
			this.getOrderList()
		},
		onReachBottom(){  //上拉触底函数
			if(this.status == 'more'){  //此处判断，上锁，防止重复请求
				this.page+=1
				this.getOrderList()
			}
		},
		methods:{
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.current = e.currentIndex
					this.page = 1
					this.orderList = []
					this.getOrderList()
				}
			},
			getOrderList(){
				this.status = 'loading'
				getOrderList({state:this.state[this.current],page:this.page}).then(res=>{
					this.orderList.push(...res.data.data.data)
					if(this.orderList.length >= res.data.data.total){
						this.status = 'noMore'
					}else {
						this.status = 'more'
					}
				})
			},
			// 去支付
			getPay(id){
				uni.navigateTo({url:'/pages/shop/confirmOrder/confirmOrder?id=' + id+'&type=repay'})
			},
			// 取消订单
			cancelOrder(id){
				let token = uni.getStorageSync("token")
				cancelOrder({id,token}).then(res=>{
					console.log(res)
					this.page = 1
					this.orderList = []
					this.getOrderList()
				})
			},
			// 确认收货
			confirmOrder(id){
				let token = uni.getStorageSync("token")
				confirmOrder({id,token}).then(res=>{
					console.log(res)
					this.page = 1
					this.orderList = []
					this.getOrderList()
				})
			},
			// 查看明细
			toDetail(id){
				console.log(id)
				uni.navigateTo({url:'/pages/my/orderDetail/orderDetail?id=' + id + '&state=' + this.state[this.current]})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		min-height: calc(100vh - 88rpx);
		background: #F8F8F8;
		/deep/.segmented-control {
			background-color: #fff;
		}
	}
	.orders {
		padding: 0 24rpx;
		.order {
			width: 100%;
			// height: 392rpx;
			background-color: #fff;
			padding: 24rpx 30rpx;
			box-shadow: 0px 2rpx 20rpx 0px rgba(0,0,0,0.05);
			border-radius: 16rpx;
			margin-top: 22rpx;
			.top {
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 1rpx solid #E7E7E7;
				padding-bottom: 20rpx;
				.order_no{
					font-size: 26rpx;
					font-weight: 400;
					color: #999999;
				}
				.wait_time {
					height: 42rpx;
					background-color: #FFD7D4;
					border-radius: 21rpx;
					font-size: 24rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #FFFFFF;
					line-height: 42rpx;
					padding: 0 1rpx;
					display: flex;
					.wait {
						height: 42rpx;
						padding-left: 12rpx;
						padding-right: 4rpx;
						background-color: #FA2C1E;
						border-radius: 21rpx 0 20rpx 21rpx;
					}
					.time {
						padding-left: 4rpx;
						padding-right: 12rpx;
						color: #fc5348;
					}
				}
			}
			.center {
				width: 100%;
				padding: 10rpx 0;
				display: flex;
				border-bottom: 1px solid #E7E7E7;
				.order_img {
					width: 144rpx;
					height: 164rpx;
					display: flex;
					align-items: center;
					.img {
						width: 144rpx;
						height: 100%;
					}
				}
				.center_right {
					padding-left: 28rpx;
					width: 100%;
					height: 164rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					font-family: PingFangSC-Medium, PingFang SC;
					.name {
						font-size: 28rpx;
						width: 330rpx;
						color: #222222;
						font-weight: 500;
						line-height: 40rpx;
					}
					.price_info {
						display: flex;
						justify-content: space-between;
						.price {
							color: #FA2C1E;
							line-height: 34px;
						}
						.number {
							font-size: 26rpx;
							font-family: PingFangSC-Regular, PingFang SC;
							font-weight: 400;
							color: #999999;
							line-height: 34px;
						}
					}
				}
			}
			.order_time {
				font-size: 24rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #999999;
				line-height: 34rpx;
				margin-top: 12rpx;
			}
			.options {
				margin-top: 12rpx;
				.btn {
					width: 170rpx;
					height: 60rpx;
					border-radius: 30rpx;
					font-size: 26rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					line-height:60rpx;
					float: right;
					margin-left: 20rpx;
				}
				.btn::after{
					content:none;
					border:none
				}
				.cancel {
					border: 1px solid #D1D2D5;
					color: #666666;
				}
				.pay {
					background: linear-gradient(90deg, #FF4050 0%, #FA2C1E 100%);
					color: #FFFFFF;
				}
			}
			.options::after{clear:both;content:'';display:block;}
		}
	}
</style>
